package org.jivesoftware.smack.proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;

public class Socks5ProxySocketFactory extends SocketFactory
{
  private ProxyInfo proxy;

  public Socks5ProxySocketFactory(ProxyInfo paramProxyInfo)
  {
    this.proxy = paramProxyInfo;
  }

  public Socket createSocket(String paramString, int paramInt)
    throws IOException, UnknownHostException
  {
    return socks5ProxifiedSocket(paramString, paramInt);
  }

  public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
    throws IOException, UnknownHostException
  {
    return socks5ProxifiedSocket(paramString, paramInt1);
  }

  public Socket createSocket(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    return socks5ProxifiedSocket(paramInetAddress.getHostAddress(), paramInt);
  }

  public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2, int paramInt2)
    throws IOException
  {
    return socks5ProxifiedSocket(paramInetAddress1.getHostAddress(), paramInt1);
  }

  private Socket socks5ProxifiedSocket(String paramString, int paramInt)
    throws IOException
  {
    Socket localSocket = null;
    InputStream localInputStream = null;
    OutputStream localOutputStream = null;
    String str1 = this.proxy.getProxyAddress();
    int i = this.proxy.getProxyPort();
    String str2 = this.proxy.getProxyUsername();
    String str3 = this.proxy.getProxyPassword();
    try
    {
      localSocket = new Socket(str1, i);
      localInputStream = localSocket.getInputStream();
      localOutputStream = localSocket.getOutputStream();
      localSocket.setTcpNoDelay(true);
      byte[] arrayOfByte1 = new byte[1024];
      int j = 0;
      arrayOfByte1[(j++)] = 5;
      arrayOfByte1[(j++)] = 2;
      arrayOfByte1[(j++)] = 0;
      arrayOfByte1[(j++)] = 2;
      localOutputStream.write(arrayOfByte1, 0, j);
      fill(localInputStream, arrayOfByte1, 2);
      int k = 0;
      switch (arrayOfByte1[1] & 0xFF)
      {
      case 0:
        k = 1;
        break;
      case 2:
        if ((str2 != null) && (str3 != null))
        {
          j = 0;
          arrayOfByte1[(j++)] = 1;
          arrayOfByte1[(j++)] = (byte)str2.length();
          System.arraycopy(str2.getBytes(), 0, arrayOfByte1, j, str2.length());
          j += str2.length();
          arrayOfByte1[(j++)] = (byte)str3.length();
          System.arraycopy(str3.getBytes(), 0, arrayOfByte1, j, str3.length());
          j += str3.length();
          localOutputStream.write(arrayOfByte1, 0, j);
          fill(localInputStream, arrayOfByte1, 2);
          if (arrayOfByte1[1] == 0)
            k = 1;
        }
        break;
      case 1:
      }
      if (k == 0)
      {
        try
        {
          localSocket.close();
        }
        catch (Exception localException3)
        {
        }
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "fail in SOCKS5 proxy");
      }
      j = 0;
      arrayOfByte1[(j++)] = 5;
      arrayOfByte1[(j++)] = 1;
      arrayOfByte1[(j++)] = 0;
      byte[] arrayOfByte2 = paramString.getBytes();
      int m = arrayOfByte2.length;
      arrayOfByte1[(j++)] = 3;
      arrayOfByte1[(j++)] = (byte)m;
      System.arraycopy(arrayOfByte2, 0, arrayOfByte1, j, m);
      j += m;
      arrayOfByte1[(j++)] = (byte)(paramInt >>> 8);
      arrayOfByte1[(j++)] = (byte)(paramInt & 0xFF);
      localOutputStream.write(arrayOfByte1, 0, j);
      fill(localInputStream, arrayOfByte1, 4);
      if (arrayOfByte1[1] != 0)
      {
        try
        {
          localSocket.close();
        }
        catch (Exception localException4)
        {
        }
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "server returns " + arrayOfByte1[1]);
      }
      switch (arrayOfByte1[3] & 0xFF)
      {
      case 1:
        fill(localInputStream, arrayOfByte1, 6);
        break;
      case 3:
        fill(localInputStream, arrayOfByte1, 1);
        fill(localInputStream, arrayOfByte1, (arrayOfByte1[0] & 0xFF) + 2);
        break;
      case 4:
        fill(localInputStream, arrayOfByte1, 18);
      case 2:
      }
      return localSocket;
    }
    catch (RuntimeException localRuntimeException)
    {
      throw localRuntimeException;
    }
    catch (Exception localException1)
    {
      try
      {
        if (localSocket != null)
          localSocket.close();
      }
      catch (Exception localException2)
      {
      }
      String str4 = "ProxySOCKS5: " + localException1.toString();
      if ((localException1 instanceof Throwable))
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, str4, localException1);
      throw new IOException(str4);
    }
  }

  private void fill(InputStream paramInputStream, byte[] paramArrayOfByte, int paramInt)
    throws IOException
  {
    int i = 0;
    while (i < paramInt)
    {
      int j = paramInputStream.read(paramArrayOfByte, i, paramInt - i);
      if (j <= 0)
        throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "stream is closed");
      i += j;
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.proxy.Socks5ProxySocketFactory
 * JD-Core Version:    0.6.1
 */